<template>
<!-- 遗体运返 -->
    <el-card class="box-card">
        <div slot="header" class="card-heaer">
            <span style="line-height: 36px;">{{name}}理赔录入</span>
            <el-button type="primary" style="float:right" @click="up" v-if="state!='查看'&&state!='结果'">提交</el-button>
        </div>
        <rescueInter v-if="name=='遗体运返或安葬'" :modelrules="rescueMoveRules" :newState="reportState">
        </rescueInter>
        <div class="space20">
        </div>
        <el-form ref="form" label-width="180px">
            <el-row type="flex" class="row-bg">
                <el-col :span="6">
                    <el-form-item label="遗体处理方式:">
                        <el-select v-model="BILL_HBURIAL_TYPE" placeholder="请选择">
                            <el-option label="遗体转送回国" value="灵柩费"></el-option>
                            <el-option label="火葬或运送骨灰回国" value="骨灰盒费"></el-option>
                            <el-option label="就地安葬" value="就地安葬费"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6" :offset="2" v-show="BILL_HBURIAL_TYPE!='就地安葬费' && BILL_HBURIAL_TYPE!='' ">
                    <el-form-item :label="BILL_HBURIAL_TYPE">
                        <el-input v-model="BILL_HBURIAL_MONEY"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row type="flex" class="row-bg" v-if="state=='新增'||state=='修改'">
                <el-col :span="6">
                    <el-form-item label="日期:">
                        <el-date-picker type="date" placeholder="选择日期" v-model="addtime" style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="3" :offset="2">
                    <el-button type="primary" @click="adddata">新增一天</el-button>
                </el-col>
            </el-row>
        </el-form>
        <el-select v-model="mediacalCheck" placeholder="请审核" v-if="state=='理赔审核'||state=='医师审核'">
            <el-option label="审核通过" value="需计算"></el-option>
            <el-option label="拒赔" value="理赔审核不通过"></el-option>
            <el-option label="退回重写" value="需理赔修改"></el-option>
        </el-select>
        <div class="space20">
        </div>
        <el-input v-model="checkReason" placeholder="原因" style="width:250px" :disabled="state=='查看'||state=='修改'" v-show="mediacalCheck=='需理赔修改'||mediacalCheck=='理赔审核不通过' || state=='查看' ||state=='修改'"></el-input>

        <div class="space20">
        </div>
        <el-collapse v-if="dataList.length>0">
            <el-collapse-item :title="data.show+'账单'" :key="data.index" v-for="(data, index) in dataList ">
                <el-row type="flex" class="row-bg">

                    <el-col :span="3" :offset="2" v-if="state=='新增'||state=='修改'">
                        <el-button type="primary" @click="addOther(index,data.value)">新增账单</el-button>
                    </el-col>
                </el-row>
                <div class="space20">
                </div>
                <el-card class="box-card" v-show="data.BILL_EMERGENCYOTHER.length>0">

                    <div class="space20">
                    </div>
                    <div v-for="(item,index2) in data.BILL_EMERGENCYOTHER" :key="item.BILL_EMERGENCYOTHER_ID">
                        <div class="space20">
                        </div>
                        <el-form ref="form" label-width="100px" :model="item" :inline="true">
                            <el-form-item label="项目名称" :prop="'BILL_EMERGENCYOTHER_NAME'" :rules="{
                                                                                                          required: true, message: '项目名称不能为空', trigger: 'blur'
                                                                                                        }">
                                <el-input v-model="item.BILL_EMERGENCYOTHER_NAME"></el-input>
                            </el-form-item>

                            <el-form-item label="项目金额" :prop="'BILL_EMERGENCYOTHER_MONEY'" :rules="{
                                                                                                          required: true, message: '项目金额不能为空', trigger: 'blur'
                                                                                                        }">
                                <el-input v-model="item.BILL_EMERGENCYOTHER_MONEY"></el-input>
                            </el-form-item>
                            <el-button style="float: right; margin-right:50px;" v-if="state=='新增'" type="danger" @click="delectitemOther(index,index2)">删除</el-button>

                        </el-form>
                    </div>
                </el-card>

            </el-collapse-item>
        </el-collapse>

    </el-card>
</template>
<script>
import getid from '@/js/getid';
import {
    mapState
} from 'vuex';
import rescueInter from '@/components/lipei/lipeiaddReport/rescueInter'
export default {
    data() {
        return {
            addtime: "",
            dataList: [],
            BILL_HBURIAL_MONEY: "",
            BILL_HBURIAL_TYPE: "",
            mediacalCheck: "",
            checkReason: "",
            id: "",
            reportState: ""
        }
    },
    components: {
        'rescueInter': rescueInter
    },
    computed: mapState({

        rescueInter: state => state.reportFormItem.rescueInter,
        rescueInterRules: state => state.reportFormItem.rescueInterRules,
    }),
    props: {
        name: {
            type: String,
            default: ""
        },
        state: {
            type: String,
            default: "新增"
        }
    },

    methods: {
        adddata() {

            var data = this.addtime;
            var month = data.getMonth() + 1;

            var strDate = data.getDate();
            var year = data.getFullYear();
            var time = year + "-" + month + "-" + strDate;
            var model = {
                show: time,
                value: data.toLocaleDateString(),
                BILL_EMERGENCYOTHER: []

            };

            this.addtime = "";
            this.dataList.push(model);

        },

        addOther(index, time) {
            var model = {
                BILL_EMERGENCYOTHER_ID: getid.getid(),
                BILL_EMERGENCYOTHER_NAME: "",
                BILL_EMERGENCYOTHER_MONEY: "",
                BILL_EMERGENCYOTHER_TYPE: "",
                RI_ID: "",
                BILL_EMERGENCYOTHER_OTHER: "",
                BILL_EMERGENCYOTHER_DATE: time,
                type: "其他"
            };
            this.dataList[index].BILL_EMERGENCYOTHER.push(model);

        },
        up() {
            if (this.state == "新增") {
                this.addNew();
            } else {
                this.lipeiCheck();
            }

        },
        lipeiCheck() {
            var model = {};
            var url;
            var id = getid.getid();
            url = "BILL_HBURIAL";
            if (this.state == "修改") {
                model.BILL_HBURIAL_STATE = "需理赔审核";
                model.BILL_HBURIAL_WHY = "";
             
                if (this.rescueInter.RI_HBURIAL_ADDRESS && Object.prototype.toString.call(this.rescueInter.RI_HBURIAL_ADDRESS)=='[object Array]') {
                    this.rescueInter.RI_HBURIAL_ADDRESS = this.rescueInter.RI_HBURIAL_ADDRESS.join(",");
                }
                model.RI_HBURIAL = this.rescueInter;
            } else {
                model.BILL_HBURIAL_STATE = this.mediacalCheck;
            }
            model.BILL_HBURIAL_ID = this.id;
            model.BILL_HBURIAL_WHY = this.checkReason;
            model.RI_HBURIAL_ID = this.$route.query["id"];
            model.BILL_HBURIAL_MONEY = this.BILL_HBURIAL_MONEY;
            model.BILL_HBURIAL_TYPE = this.BILL_HBURIAL_TYPE;
            var BILL_EMERGENCYOTHER = [];

            for (var i = 0; i < this.dataList.length; i++) {
                for (var k = 0; k < this.dataList[i].BILL_EMERGENCYOTHER.length; k++) {
                    this.dataList[i].BILL_EMERGENCYOTHER[k].RI_ID = this.id;
                    BILL_EMERGENCYOTHER.push(this.dataList[i].BILL_EMERGENCYOTHER[k]);
                }
            }

            model.BILL_EMERGENCYOTHER = BILL_EMERGENCYOTHER;


            this.$http.put(url, model).then(response => {

                var data = response.body;
                if (data.code == 1) {
                    this.$notify({
                        title: '审核成功',
                        message: '审核成功',
                        type: 'success'
                    });
                    this.$router.go(-1);
                } else {
                    this.$notify({
                        title: '提交失败',
                        message: '提交失败',
                        type: 'warning'
                    });
                }
            }, response => {
                this.loading = false;
                this.$notify.error({
                    title: '网络错误',
                    message: '网络错误'
                });
            });

        },
        addNew() {
            if (this.rescueInter.RI_HBURIAL_BILLDOC == "") {
                this.$notify.error({
                    title: '附件未上传',
                    message: '附件未上传'
                });
                return;
            }
            var model = {};
            var url;
            var id = getid.getid();
            url = "BILL_HBURIAL";
            model.BILL_HBURIAL_ID = id;
            model.RI_HBURIAL_ID = this.$route.query["id"];
            model.BILL_HBURIAL_STATE = "理赔审核";
            model.BILL_HBURIAL_MONEY = this.BILL_HBURIAL_MONEY;
            model.BILL_HBURIAL_TYPE = this.BILL_HBURIAL_TYPE;
            var BILL_EMERGENCYOTHER = [];

            for (var i = 0; i < this.dataList.length; i++) {
                for (var k = 0; k < this.dataList[i].BILL_EMERGENCYOTHER.length; k++) {
                    this.dataList[i].BILL_EMERGENCYOTHER[k].RI_ID = id;
                    BILL_EMERGENCYOTHER.push(this.dataList[i].BILL_EMERGENCYOTHER[k]);
                }
            }
            if (this.rescueInter.RI_HBURIAL_ADDRESS && Object.prototype.toString.call(this.rescueInter.RI_HBURIAL_ADDRESS)=='[object Array]') {
                this.rescueInter.RI_HBURIAL_ADDRESS = this.rescueInter.RI_HBURIAL_ADDRESS.join(",");
            }
            model.RI_HBURIAL = this.rescueInter;
            model.BILL_EMERGENCYOTHER = BILL_EMERGENCYOTHER;
            this.$http.post(url, model).then(response => {

                var data = response.body;
                if (data.code == 1) {
                    this.$notify({
                        title: '添加成功',
                        message: '添加成功',
                        type: 'success'
                    });
                    this.$router.go(-1);
                } else {
                    this.$notify({
                        title: '提交失败',
                        message: '提交失败',
                        type: 'warning'
                    });
                }
            }, response => {
                this.loading = false;
                this.$notify.error({
                    title: '网络错误',
                    message: '网络错误'
                });
            });

        },

        delectitemOther(index, index2) {
            this.dataList[index].BILL_EMERGENCYOTHER.splice(index2, 1);
        },
        checkGet() {
            var url = 'BILL_HBURIAL';

            this.$http.get(url + "/" + this.$route.query["id"]).then(response => {
                var data = response.body;
                if (data.code == 1) {
                    // this.$store.commit('changer', data.result[0].BILL_DATA)
                    if (data.result[0].RI_HBURIAL.RI_HBURIAL_ADDRESS) {
                        var address = data.result[0].RI_HBURIAL.RI_HBURIAL_ADDRESS.split(",");
                        address[2] = address[2] + "," + address[3];
                        address[3] = null;
                        data.result[0].RI_HBURIAL.RI_HBURIAL_ADDRESS = address;
                    }
                    this.$store.commit('rescueInterChanger', data.result[0].RI_HBURIAL)
                    var listOther = data.result[0].BILL_EMERGENCYOTHER;
                    this.id = data.result[0].BILL_HBURIAL_ID;
                    this.checkReason = data.result[0].BILL_HBURIAL_WHY;
                    this.BILL_HBURIAL_MONEY = data.result[0].BILL_HBURIAL_MONEY;
                    this.BILL_HBURIAL_TYPE = data.result[0].BILL_HBURIAL_TYPE;
                    var s = new Set();
                    var sname = new Set();

                    if (listOther != null) {
                        for (var i = 0; i < listOther.length; i++) {
                            s.add(listOther[i].BILL_EMERGENCYOTHER_DATE); // showlist[]
                            sname.add(listOther[i].BILL_EMERGENCYOTHER_DATE);
                        }
                    }

                    var list = [];

                    var sarry = Array.from(s);
                    var snamearry = Array.from(sname);
                    for (var i = 0; i < sarry.length; i++) {

                        var model = {
                            show: snamearry[i],
                            value: sarry[i],
                            BILL_EMERGENCYOTHER: []

                        };
                        list.push(model);
                    }

                    if (listOther != null) {
                        for (var i = 0; i < listOther.length; i++) {
                            for (var k = 0; k < list.length; k++) {
                                if (listOther[i].BILL_EMERGENCYOTHER_DATE == list[k].value) {
                                    list[k].BILL_EMERGENCYOTHER.push(listOther[i]);
                                }
                            }
                        }

                    }
                    this.dataList = list;
                }

            }, response => {
                this.$notify.error({
                    title: '网络错误',
                    message: '网络错误'
                });
                this.loding = false;
            });
        }
    },
    activated() {
        this.mediacalCheck = "";
        this.checkReason = "";
        this.$store.commit('rescueInterChanger', {})
        if (this.state == "新增" || this.state == "修改") {
            this.reportState = '待办任务';
        }
        if (this.state == "理赔审核" || this.state == "医师审核" || this.state == "查看" || this.state == "结果") {
            this.reportState = '审核';
        }
        this.dataList = [];
        //新增/医师审核/理赔审核/修改
        if (this.state != "新增") {
            this.checkGet();
        }
        this.$http.get("Ri_H/?id=" + this.$route.query["id"] + "&type=遗体运返或安葬").then(response => {
            var data = response.body;
            if (data.code == 1) {
                if (data.result[0].RI_HBURIAL_ADDRESS) {
                    var address = data.result[0].RI_HBURIAL_ADDRESS.split(",");
                    address[2] = address[2] + "," + address[3];
                    address[3] = null;
                    data.result[0].RI_HBURIAL_ADDRESS = address;
                }
                this.$store.commit('rescueInterChanger', data.result[0])
            }

        }, response => {
            this.$notify.error({
                title: '网络错误',
                message: '网络错误'
            });
        })

    }

}
</script>

